package com.example.security.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * @author 杨胖胖
 */
@RestController
public class CaptchaController {

    @GetMapping("captcha")
    public String captcha() {
        return "success";
    }

    @GetMapping("token-info")
    public Authentication get(Authentication authentication, HttpServletRequest request) {
        String header = request.getHeader("Authentication");
        String token = StringUtils.substringAfter(header, "bearer");
        Claims claims = Jwts.parser().setSigningKey("one eternity later".getBytes(StandardCharsets.UTF_8)).parseClaimsJws(token).getBody();
        Object userId = claims.get("userId");
        return authentication;
    }
}
